package com.allianz.webapps.controllers;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.allianz.frame.utils.PropertiesUtil;
import com.allianz.frame.utils.StringUtil;
import com.allianz.webapps.https.interfaces.mail.MailUtil;
import com.allianz.webapps.https.interfaces.type001.pojo.User;

//http://localhost:8080/pad_marketing/main/sendMail.action

@Controller
@RequestMapping(value="main")
public class SendMailController {

	protected final Logger logger = LoggerFactory.getLogger(getClass());
	/**
	 * 邮件发送
	 */
	@RequestMapping(value="sendMail")
	@ResponseBody
	public String sendMail(HttpServletRequest request,HttpServletResponse response)
			throws Exception{
		
		User user =(User)request.getSession().getAttribute("username");
		String receiver=request.getParameter("receiver");	//收件人
		String mail=request.getParameter("mail");	//营销员邮箱
		String recommen=request.getParameter("recommen");	//建议书
		String name=request.getParameter("name");			//客户姓名  正文的时候会用到
		String actionUrl=request.getParameter("actionUrl");	// 正文里超链接的链接地址
		String subject=name+"的建议书";			//邮件标题 
		logger.debug("=======收件人："+receiver+"============建议书："+recommen+"===========投保人："+name+"===========链接地址："+actionUrl);
		logger("=======收件人："+receiver+"============建议书："+recommen+"===========投保人："+name+"===========链接地址："+actionUrl+"===========发件人邮箱："+user.getEmail());
		if(!StringUtil.notNull(receiver)){
			throw new Exception("邮箱名称输入错误！");
		}
		//Mail mail = new Mail();  
		boolean flag=false;
		//配置文件读取
	    
	    
	    String message="<div style=\"border:1px solid #019be1; background: #f9f9f9; padding:15px 20px; width:700px; font-size:16px; line-height:200%;\">";
	    message+="<h3>尊敬的 <span style=\"color:#ff5400\">"+name+"</span> 先生/女士，</h3>";
	    message+="<div>您的安联寿险顾问<span style=\"color:#019be1\">"+user.getAgentName()+"</span>为您定制的建议书，请您参阅以下链接：";
	    message+="</div>";
	    message+="<div><a href='"+actionUrl+"'> "+recommen+"</a></div>";		// 用建议书名称作链接
	    message+="<div style=\"font-size:14px;\">温馨提示：<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请勿直接回复本邮件,如对建议书有问题,请发送问题至您的安联寿险顾问邮箱："+mail+"</div>";
	    message+="<div class=\"height:20px;\">&nbsp;</div>";
	    message+="<div style=\"color:#ff5400\">祝一切顺利！</div>";
	    message+="<div>"+user.getAgentName()+"</div>";
	    String tel=user.getAgentTel();
	    if(StringUtil.notNull(tel)){
	    	message+="<div>手机:&nbsp;&nbsp;&nbsp;<span style=\"color:#019be1\">"+tel+"</span></div>";
	    }
	    message+="<div>中德安联保险有限公司"+user.getBranchName()+"</div>";
	    message+="<div><img src=\""+PropertiesUtil.getMailImage()+"\" alt=\"\" style=\"width:700px;\"></div>";
	    message+="</div>";
	    
	    String fileName = subject+".pdf";
	    
	    String end=actionUrl.substring(actionUrl.indexOf("?"));
	    String quoPdfURLPrefix=PropertiesUtil.getProperty("quoPdfURLPrefix");
	    String path=quoPdfURLPrefix+"QuotationPDF"+end;
	    
//	    String pdfURL = PropertiesUtil.getProperty("interfaceIP");
//	    String path=pdfURL+"";
//	    String path="http://dev.allianz.com.cn/PortalDev/Interface/iPad/OtherSignDocuments?orderId=327122&appId=353603&sign=A9CC8F483B7CB71CF6460E76F892967F";
	    
	    logger.debug("===========pdf链接路径："+path);
	    logger("===========pdf链接路径："+path);
	    
	    flag=MailUtil.sendEmail(subject, path, fileName, receiver, message);
	    
	    return flag+"";
	}
	//自定义打印日志
	private void logger(String s) throws IOException{
/*		String debugFile = PropertiesUtil.getResourcePath()+"debug.txt";
		FileWriter fww = new FileWriter(debugFile,true);
		BufferedWriter bw = new BufferedWriter(fww);

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		bw.write(df.format(new Date())+"    "+s);
		bw.newLine();
		bw.flush();
		bw.close();*/
	}
	
}
